<?php
/**
 * Plugin Name: Fresh Post
 * Plugin URI: http://sites.google.com/site/giacatlong87/
 * Description: A set of posts that get updated daily, the list appears in chronological order based on the newest update
 * Version: 1.0
 * Author: nclong
 * Author URI: http://fire-studios.com
 * License: GPL2
 * .
 * This plugin and it's accompanying tutorial are written for Dooms at http://net.tutsplus.com
 * .
 */
 global $wpdb; // call global so we can get the database prefix
 
 function fresh_display() {
    global $wpdb, $ppbv_tablename,$post; // call global for use in function
    /* $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->postmeta.meta_key = 'tag' 
    AND $wpdb->postmeta.meta_value = 'email' 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
	";

	$pageposts = $wpdb->get_results($querystr, OBJECT);

    echo "<div id='popular_by_views'>"; // create a container
        echo "<h2>Most Popular by Views</h2>"; // write the title
        echo "<ol id='popular_by_views_list'>"; // create an ordered list
            $popular = $wpdb->get_results("SELECT * FROM {$ppbv_tablename} ORDER BY views DESC LIMIT 0,10",ARRAY_N);
            foreach($popular as $post){ // loop through the returned array of popular posts
                $ID = $post[1]; // store the data in a variable to save a few characters and keep the code cleaner
                $views = number_format($post[2]); // number_format adds the commas in the right spots for numbers (ex: 12543 to 12,543)
                $post_url = get_permalink($ID); // get the URL of the current post in the loop
                $title = get_the_title($ID); // get the title of the current post in the loop
                echo "<li><a href='{$post_url}'>{$title}</a> - {$views} views</li>"; // echo out the information in a list-item
            } // end the loop
        echo "</ol>"; // close the ordered list
    echo "</div>"; // close the container */
	//echo "Hello World 2!";
	$fresh_post = get_option('freshposts_number');
	if($fresh_post==null)
		$fresh_post = 7;
	query_posts(array( 'category_name' => 'fresh-articles', 'posts_per_page' => $fresh_post, 'orderby' => 'post_date', 'order' => 'DESC' ));
	echo '<div id="fresh_div">';
	$i = 0;
	while (have_posts()) : the_post(); 
		$bgcolor = '';
		if($i%2==0)
			$bgcolor = ' style="background-color:#fff" ';
		echo "<div class='fresh_link' $bgcolor><a href=\"".get_permalink($post->ID)."\" title=\"$post->post_title\">".trimString($post->post_title,20).'</a></div>';
		$i++;
	endwhile;
	echo '</div>';

}